<template>
	<view class="new-users copy-data">
		<view class="mid" style="flex:1;overflow: hidden;">
			<scroll-view scroll-y="true" style="height: 100%;">
				<view class="bg"></view>
				<view class="head pad30">
					<view class="user-card">
						<!-- #ifdef APP-PLUS -->
						<view @click="scanQRCode" style="position: absolute;top: -30rpx;right: 10px;z-index: 99;">
							<image style="width: 40px;height: 40px;" src="/static/img/sys_o.png"></image>
						</view>
						<!-- #endif -->

						<view class="user-info">
							<image class="avatar" :src='userInfo.avatar' v-if="userInfo.avatar && uid"
								@click="goEdit()"></image>
							<image v-else class="avatar" src="/static/images/f.png" mode="" @click="goEdit()"></image>
							<view class="info">
								<view class="name" @tap="openAuto">
									{{ userInfo.nickname }}
								</view>
								<view class="name" style="font-size: 32rpx;">
									{{ userInfo.phone }}
								</view>

								<view class="label">
									<view class="leve"> 
									 <text v-if="userInfo.level == 0">普通会员</text> 
									 <text v-if="userInfo.level == 1">门店</text> 
									 <text v-if="userInfo.level == 2">代理</text> 
									 <text v-if="userInfo.level == 3">合伙人</text> 
									 <text v-if="userInfo.level == 4">董事</text> 
									 <text v-if="userInfo.level == 5">股东</text> 
									</view>
								</view>
							</view>
							<!-- 邀请码 -->
							<view class="yqm">
								邀请码 {{ userInfo.inviteCode }}
							</view>
						</view>
					</view>

					<view class="order-wrapper">
						<view class="order-bd">
							<navigator class="order-item" hover-class="none" url="/pages/users/goldtrading/healthyBean">
								<text class="order-status-num">{{userInfo.money}}</text>
								<view class="txt">余额</view>
							</navigator>
							<!-- <navigator class="order-item" hover-class="none" url="">
									<text class="order-status-num" >{{userInfo.devote}}</text>
									<view class="txt">贡献值</view>
								</navigator> -->
							<navigator class="order-item" hover-class="none"
								>
								<text class="order-status-num">{{userInfo.score}}</text>
								<view class="txt">积分</view>
							</navigator>
							<!-- 	<navigator class="order-item" hover-class="none" url="">
									<text class="order-status-num" >{{userInfo.entityMoney}}</text>
									<view class="txt">消费金</view>
								</navigator> -->

						</view>
					</view>

					<!-- listmenu -->
					<view class="menuList">
						<block v-for="(item,index) in menu" :key="index">
							<view @click="blackHand(item)" class="menu-item">
								<image :src="item.img" mode=""></image>
								<view class="txt">
									{{item.title}}
								</view>
							</view>
						</block>
					</view>
					<view class="list-card">
						<block v-for="(item,index) in listMenu" :key="index">
							<view @click="blackHand(item)" class="list-item">
								<view class="left">
									<image class="image" :src="item.img" mode=""></image>
									<view class="txt">{{item.title}}</view>
								</view>
								<image class="right" src="/static/images/right.png" mode="">
							</view>
						</block>
					</view>
				</view>
			</scroll-view>
		</view>

		<wu-app-update></wu-app-update>
	</view>
</template>
<script>
	let sysHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
	import Cache from '@/utils/cache';
	import {
		BACK_URL
	} from '@/config/cache';
	import {
		getMenuList,
		getBusiness
	} from '@/api/user.js';
	import {
		orderData
	} from '@/api/order.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		getCity
	} from '@/api/api.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef H5
	import Auth from '@/libs/wechat';
	// #endif
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	const app = getApp();
	export default {
		components: {
			// #ifdef MP
			//authorize
			// #endif
		},
		computed: mapGetters(['isLogin', 'chatUrl', 'userInfo', 'uid']),
		data() {
			return {
				isM: false, //是否为商家 
				orderMenu: [],
				menu: [
					//{
					//img: '/static/images/lmsj.png',
					//	title: '联盟商家',
					//	url: '/pages/shop/store/submit/submit',
					//},
					{
						img: '/static/images/lcsy.png',
						title: '联创收益',
						// url: '/pages/users/teams/index?status=1',
					},
					// {
					// 	img: '/static/images/wdtd.png',
					// 	title: '我的团队',
					// 	url: '/pages/users/teams/index',
					// },
					{
						img: '/static/images/wddd.png',
						title: '我的订单',
						url: '/pages/users/order_list/index?status=2',
					},
					// {
					// 	img: '/static/images/wdaz.png',
					// 	title: '我的安置',
					// 	url: '/pages/users/placement/placement',
					// },
					// {
					// 	img: '/static/images/skm.png',
					// 	title: '收款码',
					// 	isQr: true,
					// 	url: '/pages/users/moneyReceiving/index',
					// },
					{
						img: '/static/images/yqhy.png',
						title: '邀请好友',
						url: '/pages/users/invation/index',
					},

					// {
					// 	img: '/static/images/tzjy.png',
					// 	title: '数字通证',
					// 	url: '/pages/scanCodePages/download/sztz_app',
					// },
					{
						img: '/static/images/lmsj.png',
						title: '分红中心',
						url: '/pages/users/my_category/index',
					},
					{
						img: '/static/images/tzjy.png',
						title: '提现',
						url: '/pages/users/user_cash/index',
					},
				],

				listMenu: [{
						img: '/static/images/kf.png',
						title: '在线客服',
						url: '/pages/customer_list/index?status=2',
					},
					{
						img: '/static/images/setting.png',
						title: '设置',
						url: '/pages/users/setting/index',
					},
				],

				imgUrls: [],
				userMenu: [],
				autoplay: true,
				circular: true,
				interval: 3000,
				duration: 500,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				orderStatusNum: {},
				MyMenus: [],
				wechatUrl: [],
				servicePic: '/static/images/customer.png',
				sysHeight: sysHeight,
				config:{},
				// #ifdef MP
				pageHeight: '100%',
				// #endif
				// #ifdef H5
				pageHeight: app.globalData.windowHeight,
				// #endif
				// #ifdef H5
				isWeixin: Auth.isWeixin()
				//#endif
			}
		},
		onLoad() {
			console.log(123);
			this.searchisMerchant()
			let that = this;
			// #ifdef H5
			that.$set(that, 'pageHeight', app.globalData.windowHeight);
			// #endif
			that.$set(that, 'MyMenus', app.globalData.MyMenus);
			if (!this.$Cache.has('cityList')) this.getCityList();
			if (that.isLogin == false) {
				uni.navigateTo({
					url: '/pages/users/login/index'
				})
				// #ifdef H5
				toLogin()
				// #endif
			}
		},
		onShow: function() {
			this.config = uni.getStorageSync("pay_type_config");
			this.orderMenu = []
			this.orderMenu.push({
				title: '分红',
				url: '/pages/users/user_cash/commission',
				num: this.userInfo.money
			}, {
				title: '贡献值',
				// url: '/pages/users/order_list/index?status=1',
				num: this.userInfo.devote
			}, {
				title: '通证',
				url: '/pages/users/goldtrading/list?status=2',
				num: this.userInfo.score
			})


			let that = this;
			// #ifdef H5
			uni.getSystemInfo({
				success: function(res) {
					that.pageHeight = res.windowHeight + 'px'
				}
			});
			// #endif
			if (that.isLogin) {
				this.getMyMenus();
				// this.setVisit();
				this.getOrderData();
				this.$store.dispatch('USERINFO');
			} else {
				// toLogin();
			}


		},
		methods: {
			// 查询是否是商家 
			searchisMerchant() {
				getBusiness().then((res) => {
					this.isM = res.data > 0 ? true : false
				})
			},
			blackHand(item) {
				if (item.title == '联创收益') {
					this.$util.Tips({
						title: '您尚未达到权限'
					});
					return
				}
				if (item.title == '提现') {
					if(this.config.applyTx == 1){
						this.$util.Tips({
							title: this.config.message
						});
						return
					}
					
				}
				if (item.isQr) {
					uni.navigateTo({
						url: `${item.url}?isM=${this.isM}`
					})
				} else {
					uni.navigateTo({
						url: item.url
					})
				}

			},
			scanQRCode() {
				uni.scanCode({
					success: (res) => {
						console.log('扫描结果：' + res.result);
						// uni.showToast({
						// 	title:res.result,
						// 	icon:'none'
						// })
						let resu = decodeURIComponent(res.result)
						let results = JSON.parse(resu)
						let type = results.type
						let invite_code = results.uid
						let money = results.money
						console.log(money)
						if (type == 1 || type == 3) {
							uni.navigateTo({
								url: "/pages/users/moneyReceiving/pay?invite_code=" + invite_code +
									"&money=" + money + "&type=" + type
							})
						}
					},
					fail: (err) => {
						console.error('扫描失败：' + err);
						// uni.showToast({
						// 	title: '',
						// 	icon: 'none'
						// });
					}
				});
			},

			navito(e) {
				window.location.href = 'https://' + e;
			},
			kefuClick() {
				location.href = this.chatUrl;
			},
			getOrderData() {
				let that = this;
				orderData().then(res => {
					that.orderMenu.forEach((item, index) => {
						switch (item.title) {
							case '待付款':
								item.num = res.data.unPaidCount
								break
							case '待发货':
								item.num = res.data.unShippedCount
								break
							case '待收货':
								item.num = res.data.receivedCount
								break
							case '待评价':
								item.num = res.data.evaluatedCount
								break
							case '售后/退款':
								item.num = res.data.refundCount
								break
						}
					})
					that.$set(that, 'orderMenu', that.orderMenu);
				})
			},
			// 打开授权
			openAuto() {
				Cache.set(BACK_URL, '')
				toLogin();
			
				
			},
			// 授权回调
			onLoadFun() {
				this.getMyMenus();
				// this.setVisit();
				this.getOrderData();
			},
			Setting: function() {
				uni.openSetting({
					success: function(res) {
						console.log(res.authSetting)
					}
				});
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			// 绑定手机
			bindPhone() {
				uni.navigateTo({
					url: '/pages/users/app_login/index'
				})
			},
			/**
			 * 
			 * 获取个人中心图标
			 */
			getMyMenus: function() {
				let that = this;
				if (this.MyMenus.length) return;
				getMenuList().then(res => {
					that.$set(that, 'MyMenus', res.data.routine_my_menus);
					that.wechatUrl = res.data.routine_my_menus.filter((item) => {
						return item.url.indexOf('service') !== -1
					})
					res.data.routine_my_menus.map((item) => {
						if (item.url.indexOf('service') !== -1) that.servicePic = item.pic
					})
					if (res.data.routine_my_banner) {
						that.imgUrls = res.data.routine_my_banner
					}
				});
			},
			// 编辑页面
			goEdit() {
				if (this.isLogin == false) {
					console.log(this.isLogin)
					toLogin();
				} else {
					uni.navigateTo({
						url: '/pages/users/user_info/index'
					})
				}
			},
			// 签到
			goSignIn() {
				uni.navigateTo({
					url: '/pages/users/user_sgin/index'
				})
			},
			// goMenuPage
			goMenuPage(url) {
				if (this.isLogin) {
					uni.navigateTo({
						url
					})
				} else {
					uni.navigateTo({
						url: '/pages/users/login/index'
					})
					// #ifdef MP
					//this.openAuto()
					// #endif
				}
			},
			// 获取地址数据
			getCityList: function() {
				let that = this;
				getCity().then(res => {
					let oneDay = 24 * 3600 * 1000;
					this.$Cache.setItem({
						name: 'cityList',
						value: res.data,
						expires: oneDay * 7
					}); //设置七天过期时间
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page,
	body {
		height: 100%;
	}

	.bg {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 420rpx;
		background-image: url('~@/static/images/user_bg.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;
	}

	.contenBox {
		padding: 0 30rpx;
	}

	.support {
		width: 219rpx;
		height: 74rpx;
		margin: 54rpx auto;
		display: block;
	}

	.new-users {
		display: flex;
		flex-direction: column;
		height: 100%;

		.head {
			.user-card {
				position: relative;
				width: 100%;
				margin: 210rpx 0 auto;
				transform: translateY(-50%);

				.user-info {
					z-index: 20;
					position: relative;
					display: flex;
					align-items: center;
					justify-content: start;

					.avatar {
						width: 130rpx;
						height: 130rpx;
						background: white;
						border-radius: 50%;
					}

					.info {
						display: flex;
						flex-direction: column;
						margin-left: 20rpx;
						padding: 15rpx 0;

						.name {
							display: flex;
							align-items: center;
							color: #fff;
							font-size: 36rpx;
							font-weight: bold;
						}

						.label {
							display: flex;
							margin-top: 13rpx;

							.leve {
								width: 88rpx;
								height: 31rpx;
								line-height: 31rpx;
								border-radius: 32rpx;
								text-align: center;
								font-size: 17rpx;
								color: white;
								margin-right: 14rpx;
								background: rgba(255, 255, 255, .32);
							}
						}

					}

					.yqm {
						color: white;
						font-size: 26rpx;
						font-weight: bold;
						margin-left: 66rpx;
					}
				}
			}

			// 佣金 贡献值  金币
			.order-wrapper {
				background-color: #fff;
				border-radius: 14rpx;
				padding: 33rpx 16rpx;
				position: relative;
				z-index: 11;


				.order-bd {
					display: flex;
					justify-content: space-around;

					.order-item {
						width: 100%;
						border-right: 1rpx solid rgba(65, 136, 241, .43);
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;

						.txt {
							font-size: 26rpx;
							color: #4188F1;
							font-size: 26rpx;
						}
					}

					.order-item:last-child {
						border-right: 1rpx solid transparent;
					}
				}
			}
		}

		.order-status-num {
			min-width: 42rpx;
			color: #4188F1;
			right: -14rpx;
			top: -15rpx;
			font-size: 30rpx;
			font-weight: bold;
		}
	}

	.list-card {
		width: 100%;

		.list-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 90rpx;
			width: 100%;
			padding: 20rpx 25rpx;
			background-color: #fff;
			margin-top: 20rpx;
			font-size: 34rpx;

			.left {
				display: flex;
				align-items: center;
				flex: 1;

				.image {
					width: 20px;
					height: 20px;
					margin-right: 20rpx;
				}
			}

			.right {
				width: 17rpx;
				height: 31rpx;
			}
		}

	}

	.txt {
		font-size: 28rpx;
	}

	.menuList {
		display: flex;
		flex-wrap: wrap;
		background-color: white;
		border-radius: 14rpx;
		margin-top: 20rpx;
		padding: 30rpx 20rpx 0 20rpx;

		.menu-item {
			flex: 0 0 25%;
			display: flex;
			align-items: center;
			flex-direction: column;
			justify-content: center;
			margin-bottom: 30rpx;

			image {
				width: 84rpx;
				height: 84rpx;
			}

			.txt {
				margin-top: 10rpx;
			}
		}
	}
</style>